package metaObject;

import javassist.CannotCompileException;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.NotFoundException;
import javassist.Translator;
import javassist.tools.reflect.Loader;

/**
 * Definition d'un nouveau translator
 * @author 3003064_3153319
 *
 */
public class MyMetaTranslator implements Translator{

	/**
	 * Loader
	 */
	Loader lo;
	
	/**
	 * Constructeur
	 * @param l : Loader
	 */
	public MyMetaTranslator(Loader l){
		this.lo=l;
	}
	
	/* 
	 * @see javassist.Translator#onLoad(javassist.ClassPool, java.lang.String)
	 * 
	 */
	@Override
	public void onLoad(ClassPool pool, String classname) throws NotFoundException,
			CannotCompileException {
		
		if(classname.contains("metaObject")||
			classname.contains("stores")||
				classname.contains("junit")|| 
				classname.contains("annotation")|| 
				classname.contains("javassist.tools.reflect")|| 
				classname.startsWith("org.")|| 
				classname.endsWith(".class") || classname.startsWith("sun.reflect")){
			return;
		}
		
		//Recupere la class chargee
		CtClass myCtClass = pool.get(classname);
		//Rendre reflective les super class de myCtClass 
		while(myCtClass.getSuperclass() != null && !myCtClass.getSuperclass().getName().contains("java.lang.Object")){
			myCtClass = myCtClass.getSuperclass();
			System.out.println(myCtClass.getName());
			//makeReflective
			lo.makeReflective(myCtClass.getName(), "metaObject.MyMetaObject", "javassist.tools.reflect.ClassMetaobject");
		}
		System.out.println(classname);
		lo.makeReflective(classname, "metaObject.MyMetaObject", "javassist.tools.reflect.ClassMetaobject");
	}

	@Override
	public void start(ClassPool arg0) throws NotFoundException,CannotCompileException {}

}
